package com.cnswhy.cloud.server.merchant.controller.order;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.request.PageParams;
import com.cnswhy.cloud.common.core.entity.CurrentUser;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.utils.CnswhyUtil;
import com.cnswhy.cloud.server.core.dto.ShopOrderPageDTO;
import com.cnswhy.cloud.server.core.dto.ShopOrderSaveDTO;
import com.cnswhy.cloud.server.core.dto.ShopOrderUpdateDTO;
import com.cnswhy.cloud.server.core.entity.ShopOrder;
import com.cnswhy.cloud.server.core.entity.ShopOrderDetail;
import com.cnswhy.cloud.server.core.entity.ShopOrderReceiver;
import com.cnswhy.cloud.server.core.service.ShopOrderDetailService;
import com.cnswhy.cloud.server.core.service.ShopOrderReceiverService;
import com.cnswhy.cloud.server.core.service.customer.CustomerReceiverService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.shop.ShopOrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 商品订单表
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-25
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/shopOrder")
@Api(value = "ShopOrder", tags = "商品订单表")
//@PreAuth(replace = "shopOrder:")
public class ShopOrderController extends SuperController<ShopOrderService, Long, ShopOrder, ShopOrderPageDTO, ShopOrderSaveDTO, ShopOrderUpdateDTO> {
    @Autowired
    ShopOrderDetailService shopOrderDetailService;
    @Autowired
    CustomerReceiverService customerReceiverService;
    @Autowired
    ShopOrderReceiverService shopOrderReceiverService;
    @Autowired
    CustomerService customerService;
    @Override
    public void handlerResult(IPage<ShopOrder> page) {
        for(ShopOrder shopOrder : page.getRecords()){
            shopOrder.setShopOrderDetails(shopOrderDetailService.list(Wraps.<ShopOrderDetail>lbQ().eq(ShopOrderDetail::getOrderId,shopOrder.getId())));
            shopOrder.setShopOrderReceiver(shopOrderReceiverService.getOne(Wraps.<ShopOrderReceiver>lbQ().eq(ShopOrderReceiver::getOrderId,shopOrder.getId())));
            shopOrder.setCustomer(customerService.getById(shopOrder.getCustomerId()));
        }
    }
    @Override
    public void handlerQueryParams(PageParams<ShopOrderPageDTO> params) {
        /*CurrentUser currentUser = CnswhyUtil.getCurrentUser();
        log.info(JSON.toJSONString(currentUser));
        if(currentUser != null && currentUser.getMerchantId() != null){
            params.getModel().setMerchantId(currentUser.getMerchantId());
        }*/
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ShopOrder> shopOrderList = list.stream().map((map) -> {
            ShopOrder shopOrder = ShopOrder.builder().build();
            return shopOrder;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(shopOrderList));
    }
}
